<?php session_start(); 
require("./templates/comun.php");
?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
$caract = array("-", " ", "|", "'", ".", ",", "*", ")", "(", "$", "/");


if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
 $codigograbar=	str_replace($caract, "",$_POST['codigo_art'] );
 
 /* aqui ponemos la validacion para revisar si el codigo fue insertado previamente o mejor ponemos un ajax para validar despues de que captura el codigo*/
  $insertSQL = sprintf("INSERT INTO swpr_codigossugeridos (codigo_art, cs_aplicacion, cs_marca, cs_observacion, cs_volumencompra, cs_periodo, cs_numcte, cs_numsocio,
cs_nombre, cs_email,  cs_region, cs_status)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, 0, 0)",
                       GetSQLValueString($codigograbar, "text"),
                       GetSQLValueString($_POST['aplicacion'], "text"),
                       GetSQLValueString($_POST['marca'], "text"),
                       GetSQLValueString($_POST['observacion'], "text"),
                       GetSQLValueString($_POST['volumen'], "int"),
                       GetSQLValueString($_POST['periodo'], "int"),
                       GetSQLValueString($cvecliente, "int"),
                       GetSQLValueString($cvesocio, "int"),
                       GetSQLValueString($_POST['nombreqs'], "text"),
					        GetSQLValueString($_POST['email'], "text")					   
					   );
 
   $conexion=Obtener_Conexion();
   $res=pg_query($conexion,$insertSQL)  or die ("Error linea: 52 - $insertSQL");
	
	$creado = pg_affected_rows($res);
	Desconectar($conexion);
	
/***********************/
//Envio de correo

$to="ivan.garcia@morsa.com.mx";
$tocc2  = 'rgonzalez72@hotmail.com';
$subject="Codigos Sugeridos";

$message = '
<HTML>
<HEAD>
  <TITLE>Registro Codigo Sugerido</TITLE>
</HEAD>
<BODY>
  <BR>
  <TABLE BORDER="1" CELLPADDING="5" WIDTH="500px" ALIGN="CENTER">
    <TR><th colspan="2">Mensaje recibidos a través de la pagina web morsa.com.mx</th></TR>
    <TR>
      <TD><strong>Nombre :</strong> </TD>
	  <TD>'.$_POST['nombreqs'].'</TD>
    </TR>
	 <TR>
	  <TD><strong>E-mail : </strong></TD>
	   <TD>'.$_POST['email'].'</TD>
    </TR>
    <TR>
      <TD><strong>Codigo : </strong></TD><td> '.$codigograbar.' </td>
    </TR>

	<TR>
      <TD><strong>Aplicacion: </strong></TD>
	  <TD>'.$_POST['aplicacion'].'</TD>
    </TR>
	<TR>
      <TD><strong>Marca:</strong> </TD>
	  <TD>'.$_POST['marca'].'</TD>
    </TR>
	<TR>
      <TD><strong>Volumen compra:</strong> </TD>
	  <TD>'.$_POST['volumen'].'</TD>
    </TR>
	<TR>
      <TD><strong>Periodo compra:</strong> </TD>
	  <TD>'.$_POST['periodo'].'</TD>
    </TR>
	<TR>
      <TD><strong>Cliente:</strong> </TD>
	  <TD>'.$cvecliente.'</TD>
    </TR>
	<TR>
      <TD><strong>Socio:</strong> </TD>
	  <TD>'.$cvesocio.'</TD>
    </TR>
	<TR>
      <TD colspan="2"><strong>Comentario:</strong> </TD>
	  
    </TR>
	<TR>
      <Td COLSPAN="2">'.$_POST['observacion'].'</Td>
    </TR>
    
  </TABLE>
</BODY>
</HTML>
';

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.$nombre.' <'.$email.'>' . "\r\n";

// Additional headers

// Mail it
//mail($tocc, $subject, $message, $headers);
mail($tocc2, $subject, $message, $headers);
mail($to, $subject, $message, $headers);
	
/***********************/	
	
	header('Location: inserta_codigossugeridos.php');
	  //echo $creado."|".$insertSQL;

}else{

?>
<LINK REL="stylesheet" TYPE="text/css" HREF="css/style.css?ms=<?php echo time();?>">
<form action="inserta_codigossugeridos.php" method="post" enctype="application/x-www-form-urlencoded">
<table border="0" id="pag_170" cellspacing="0" cellpadding="0" style="width:100%;height:100%;" summary="Estructura">
<!-- <tr>
	<td style="vertical-align:top;">
		<div style="background-color:#84AEC9;padding:1px 1px 1px 1px;">
			<span class="EBDSTYLE_722">Captura&nbsp;de&nbsp;Codigos&nbsp;Sugeridos</span>
		</div>
	</td>
</tr> -->
<tr>
	<!-- <td style="vertical-align:top;height:100%;background-color:#D9D9D9;border-top-color:#E6E6E6;border-top-style:solid;border-top-width:1px;border-left-color:#E6E6E6;border-left-style:solid;border-left-width:1px;border-bottom-color:#808080;border-bottom-style:solid;border-bottom-width:1px;border-right-color:#808080;border-right-style:solid;border-right-width:1px;"> -->
	<td style="vertical-align:top;height:100%;">
	  <table align="center">
		<tr valign="top">
		  <td nowrap align="right" class="formularioeditable">Codigo:</td>
		  <td><input type="text" name="codigo_art" id="codigo_art" value="<?php echo $_GET['maxid']; ?>" size="32" class="formularioeditable"></td>
		</tr>
		<tr valign="top">
		  <td nowrap align="right" class="formularioeditable">Aplicacion:</td>
		  <td>
		  <textarea id="aplicacion" name="aplicacion" cols="50" rows="2" class="formularioeditable"></textarea>
		  </td>
		<tr>
		<tr valign="top">
		  <td nowrap align="right" class="formularioeditable">Marca:</td>
		  <td><input type="text" name="marca" id="marca" value=" " size="32" class="formularioeditable">
		  </td>
		<tr>
		<tr valign="top">
		  <td nowrap align="right" class="formularioeditable">Periodo de compra :</td>
		  <td><select name="periodo" id="periodo" class="formularioeditable">
			<option value="0" >Seleccione opcion</option>
			<option value="1" >Semanal</option>
			<option value="2" >Mensual</option>
			<option value="3" >Semestral</option>
			<option value="4" >Anual</option>
		  </select>
		  </td>
		<tr>
		<tr valign="top">
		  <td nowrap align="right" class="formularioeditable">Volumen de compra:</td>
		  <td><input type="text" name="volumen" id="volumen" value="" size="10" class="formularioeditable"></td>
		</tr>
		<tr valign="top">
		  <td nowrap align="right" class="formularioeditable">Observacion:</td>
		  <td> <textarea id="observacion" name="observacion" cols="50" rows="3" class="formularioeditable"></textarea></td>
		</tr>
		<tr valign="top">
		  <td nowrap align="right" class="formularioeditable">Nombre de quien sugiere:</td>
		  <td><input type="text" name="nombreqs" id="nombreqs" value="" size="32" class="formularioeditable"></td>
		</tr>
		<tr valign="top">
		  <td nowrap align="right" class="formularioeditable">Email:</td>
		  <td><input type="text" name="email" id="email" value="" size="32" class="formularioeditable"></td>
		</tr>
		<tr valign="top">
		<!-- 	<td nowrap align="right">
				<input type="button" value="Regresar" class="formularioeditable" onclick="Go('list_sucursales.php');return false;">
			</td> -->
			<td>
				<input type="submit" value="Grabar" class="formularioeditable" >
			</td>
		</tr>
	  </table>
	  <input type="hidden" name="MM_insert" value="form1">
</form>
<?php
}
?>
